package com.java.basis.thread.cas.atomic;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReference;

/**
 * ABA问题
 *
 * @author wangning
 * @create 2022-01-28 15:40
 */
@Slf4j(topic = "c.Test02")
public class TestABA02 {
	static AtomicReference<String> ref = new AtomicReference<>("A");

	public static void main(String[] args) throws InterruptedException {
		log.debug("main start ...");
		String prev = ref.get();
		other();
		Thread.sleep(1000);
		log.debug("change A->C {}", ref.compareAndSet(prev, "C"));

	}
	private static void other() throws InterruptedException {
		new Thread(() -> {
			String prev = ref.get();
			log.debug("change A->B {}", ref.compareAndSet(prev, "B"));

		},"t1").start();
		Thread.sleep(500);

		new Thread(() -> {
			String prev1 = ref.get();
			log.debug("change B->A {}", ref.compareAndSet(prev1, "A"));

		}, "t2").start();

	}
}
